#include <QCoreApplication>
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QTimer>

#include <stdio.h>

#include "datadispose.h"

QTcpServer *tcpserver;
QTcpSocket *tcpsocket;
int listnum[200];

//QTimer *timer;

//void update_DeviceNetState();

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    ReadUserData();
    for(int i = 0; i < 200; i++)
    {
        listnum[i] = -1;
    }

    for (int i = 0; i < 200; i++)
    {
        m_nUsrNums[i] = 0;
    }

    tcpserver = new QTcpServer;

    tcpserver->listen(QHostAddress::Any, 1001); // 设置服务器端端口号为1001

    printf("Qiqi Gobang Server is opening, wait for Gobang client connect: \n"); //七七五子棋服务器已开启，等待客户端连接

    m_nUsrNum = 2;

    QObject::connect(tcpserver, &QTcpServer::newConnection,
            [=]()
            {
                tcpsocket = tcpserver->nextPendingConnection();//同一个IP地址不同端口号的tcpsocket也不同，即，每个单独开启的五子棋程序的tocpsocket都不相同
                socket_list.append(tcpsocket);
                tcpserver->resumeAccepting();//继续接受新的连接

                QString ip = tcpsocket->peerAddress().toString();
                qint16 port = tcpsocket->peerPort();
                QString temp = QString("[%1:%2] connect successful").arg(ip).arg(port);

                printf("%s\n", temp.toUtf8().data());

                int length = socket_list.length();
                //printf("length:%d\n", length);
                int lisn = -1;

                for (int i = 0; i < length; i++)
                {
                    if (socket_list.at(i) == tcpsocket)
                    {
                        for (int j = 0; j < 200; j++)
                        {
                            if (listnum[j] == -1)
                            {
                                //printf("num:%d\n", i);
                                listnum[j] = i;
                                lisn = j;
                                break;
                            }
                        }
                        break;
                    }
                }

                QObject::connect(socket_list.at(listnum[lisn]), &QTcpSocket::readyRead,
                                 [=]()
                                    {
                                        QString strread = socket_list.at(listnum[lisn])->readAll();
                                        DisposeReadData(socket_list.at(listnum[lisn]), strread);
                                    }
                );
                QObject::connect(socket_list.at(listnum[lisn]), &QTcpSocket::disconnected,  //连接断开
                                 [=]()
                                    {
                                        //qDebug() << "OK";
                                        //printf("num:%d\n", listnum[lisn]);
                                        QString ip = socket_list.at(listnum[lisn])->peerAddress().toString();
                                        qint16 port = socket_list.at(listnum[lisn])->peerPort();
                                        for (int i = 0; i < USERSMAXNUM; i++)
                                        {
                                            if (m_usrdate[i].socket == socket_list.at(listnum[lisn]))
                                            {
                                                m_usrdate[i].socket = 0;
                                                break ;
                                            }
                                            if (m_usrdate[i].room != 0)
                                            {
                                                if (m_nUserRooms[m_usrdate[i].room - 1][0] == m_usrdate[i].number)
                                                {
                                                    m_nUserRooms[m_usrdate[i].room - 1][0] = 0;
                                                }
                                                else if (m_nUserRooms[m_usrdate[i].room - 1][2] == m_usrdate[i].number)
                                                {
                                                    m_nUserRooms[m_usrdate[i].room - 1][2] = 0;
                                                }
                                                m_usrdate[i].room = 0;
                                            }
                                        }

                                        socket_list.removeAt(listnum[lisn]);
                                        for (int i = 0; i < 200; i++)
                                        {
                                            if (listnum[i] > listnum[lisn]) //将后面所有大于此序号的序号减一
                                            {
                                                listnum[i]--;
                                            }
                                        }
                                        listnum[lisn] = -1;

                                        //int lengt = socket_list.length();
                                        //printf("length:%d\n", lengt);
                                        QString temp = QString("[%1:%2] disconnect").arg(ip).arg(port);
                                        printf("%s\n", temp.toUtf8().data());
                                    }
                );
            }

            );

    return a.exec();
}


